package com.yuandian.utils;

import com.yuandian.api.business_monitor.vo.ScaleVo;
import com.yuandian.enums.ScaleTypeEnum;
import com.yuandian.po.ScaleSelfAdaptiveRule;

/**
 * 获取刻度工具类
 */
public class ScaleUtil {

	public static ScaleVo getScaleData(String startTime, String endTime) {
		ScaleVo scaleVo = new ScaleVo();

		// 计算刻度类型
		Long startTimeUsec = YDateUtil.yyyy_MM_dd_HH_mm_ss(startTime) * 1000;
		Long endTimeUsec = YDateUtil.yyyy_MM_dd_HH_mm_ss(endTime) * 1000;
		long timeSpan = endTimeUsec - startTimeUsec;
		int scale = 0;
		String scaleType = "";
		if (timeSpan <= 60000L) {
			scale = 60;
		} else {
			for (ScaleSelfAdaptiveRule rule : MergeDataUtil.scaleRuleList) {
				if (timeSpan > rule.getFrom()) {
					if (rule.getTo() == null || timeSpan <= rule.getTo()) {
						scale = rule.getScale();
						break;
					}
				}
			}
		}

		// 计算间隔的单位(分钟，小时，天)
		if (scale <= 172800000L) {
			scaleType = ScaleTypeEnum.MINUTE.name();
		} else if(scale <= 2592000000L) {
			scaleType = ScaleTypeEnum.HOUR.name();
		} else {
			scaleType = ScaleTypeEnum.DAY.name();
		}

		// 根据间隔的单位(分钟，小时，天)转换为对应的间隔数值
		switch (scaleType) {
			case "MINUTE":
				scale = scale / 60;
				break;
			case "HOUR":
				scale = scale / 3600;
				break;
			case "DAY":
				scale = scale / 86400;
				break;
		}
		scaleVo.setScale(scale);
		scaleVo.setScaleType(scaleType);
		return scaleVo;
	}

}
